package br.com.helpdesk.controller;

import java.util.ArrayList;
import java.util.List;
//import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
//import javax.faces.context.FacesContext;
//import javax.faces.event.ActionEvent;

//import org.primefaces.context.RequestContext;

import br.com.helpdesk.model.Helps;

@SuppressWarnings({"unchecked","rawtypes"})
@ManagedBean(name="beanHelps")
@RequestScoped
public class HelpsBean  implements IMetodos{
	
	Helps helps = null;
	Helps selectedHelps = null;
	private List<Helps> filtrarHelps;
	
	public HelpsBean(){
		helps = new Helps();		
		selectedHelps = new Helps();
	}
	
	public Helps getHelps() {
		return helps;
	}

	public void setHelps(Helps helps) {
		this.helps = helps;
	}

	public Helps getSelectedHelps() {
		return selectedHelps;
	}

	public void setSelectedHelps(Helps selectedHelps) {
		this.selectedHelps = selectedHelps;
	}

	public List<Helps> getFiltrarHelps() {
		return filtrarHelps;
	}

	public void setFiltrarHelps(List<Helps> filtrarHelps) {
		this.filtrarHelps = filtrarHelps;
	}

	@Override
	public boolean inserir() {
		//usuario.setU_acesso(Integer.valueOf(sessaoBean.getCodigo().toString()));
		//usuario.setU_acesso(sessaoBean.getCodigo());
		DaoHelper dao = new DaoHelper(helps);			
		return dao.inserir();
	}

	@Override
	public boolean alterar() {		
		//helps.setUsuario(Integer.valueOf(sessaoBean.getCodigo().toString()));
		//helps.setUsuario(sessaoBean.getCodigo());
		helps.setCodigo(selectedHelps.getCodigo());
		DaoHelper dao = new DaoHelper(helps);			
		return dao.alterar();
	}

	@Override
	public boolean excluir() {
		DaoHelper dao = new DaoHelper(selectedHelps);			
		return dao.excluir();
	}
	
	@Override
	public List<Helps> getListar() {				
		DaoHelper dao = new DaoHelper(helps);			
		List<Helps> lista = new ArrayList<Helps>();
		List<Helps> listaRetorno = new ArrayList<Helps>();
		
		lista = dao.getListar();
		
		for (Helps help : lista) {
			Helps nLista = new Helps();
		   		nLista.setCodigo(help.getCodigo());
				nLista.setUsuario(help.getUsuario());
				nLista.setCliente(help.getCliente());
				nLista.setProjeto(help.getProjeto());
				nLista.setStatus(help.getStatus());
				nLista.setDtcad(help.getDtcad());
				listaRetorno.add(nLista);
		}
		return  listaRetorno;
	}

	@Override
	public void gravar() {
		if(selectedHelps == null) 
			selectedHelps = new Helps();
		if(selectedHelps.getCodigo() <= 0 ){
		 inserir();
		}else alterar();		
	}
	
	public void limpar(){
		selectedHelps.setCodigo(0);		
	}
	
	public void editar(){
		helps.setCodigo(selectedHelps.getCodigo());
		helps.setUsuario(selectedHelps.getUsuario());
		helps.setCliente(selectedHelps.getCliente());
		helps.setProjeto(selectedHelps.getProjeto());
		helps.setStatus(selectedHelps.getStatus());
		helps.setDtcad(selectedHelps.getDtcad());
	}
}
